<template>
	<BaseContainer>
		<div style="padding: 12px; box-sizing: border-box">
			<div>
				<TabChange v-model="selectedMonth" :list="mothList" :isBottomLine="true" :key="tabKey" @tab-click="handleClick"></TabChange>
				<aplid-base-crud
					:key="tableKey"
					ref="crud"
					class="w-100"
					v-bind="isDetail ? (isPigeonhole ? pigeonholeConfig : adiutConfig) : baseConfig"
					:paramsObj="paramsObj"
					:operateFormate="onOperateFormate"
				>
					<template v-slot:DetailDialog="{row}">
						<component :is="components[row.tableRelVal]" :isDetail="true" :editData="row" :params="row" :editMode="editMode"></component>
						<!-- <ElderStayAllowance :isDetail="true" :params="row" :editMode="editMode"></ElderStayAllowance> -->
					</template>
					<template v-slot:addDialog="{ row, show }">
						<aplid-base-crud
							ref="crud1"
							class="w-100"
							v-bind="newBaseConfig"
							:paramsObj="paramsObj1"
							:defaultTableData="tableData"
							:operateFormate="onOperateFormate"
							@selection-change="onSelectionChange"
						>
							<template v-slot:DetailDialog="{ row, show }">
								<component :is="components[row.tableRelVal]" :isDetail="true" :params="row" :editData="row" :editMode="editMode"></component>
								<!-- <ElderStayAllowance :isDetail="true" :params="row" :editMode="editMode"></ElderStayAllowance> -->
							</template>
						</aplid-base-crud>
						<div class="btn_oart">
							<el-button type="primary" @click="toSureAdd">确认添加</el-button>
							<el-button @click="handleRefresh">取消</el-button>
						</div>
					</template>
				</aplid-base-crud>
			</div>
		</div>
	</BaseContainer>
</template>

<script setup lang="ts">
import { ref, reactive, toRefs, onMounted } from 'vue'
import apiMod from '@/apiMod/index'
import { getStore } from '@/utils/store'
import Archives from '@/views/datacenter/basicData/oldman/components/Archives/index.vue'
import ElderStayAllowance from '@/views/instOperate/components/ApplyReport/components/fiveTypeElder/ElderStayAllowance.vue'
import AplidPaginate from '@/components/AplidCrud/src/AplidPaginate/index'
import TabChange from '@/views/aplid/TabChange/index.vue'
import { baseConfig, tableConfig } from './config'
import { cloneDeep } from 'lodash-es'
import {components} from '@/views/instOperate/components/ApplyReport/components/config'
let newBaseConfig = {
	...baseConfig,
	api: {
		index: 'view_ofsSubsidyApply_elderly_amount_list',
	},
	auth: {
		index: 'view_ofsSubsidyApply_elderly_amount_list',
	},
	menuBtn: [
		{
			icon: '',
			label: '查看',
			type: 'primary',
			operate: 'Detail',
			auth: 'view_ofsSubsidyApply_elderly_amount_list',
			text: true,
			show: true,
		},
	],
	tableConfig: {
		stripe: true,
		border: true,
		maxHeight: '80vh',
		data: [],
		columns: [
			{ type: 'selection', reserveSelection: true },
			{
				label: '申请人',
				prop: 'oldmanName',
			},
			{
				label: '户籍所在区',
				prop: 'crAreaName',
			},
			{
				label: '证件号码',
				prop: 'idCard',
			},
			{
				label: '性别',
				prop: 'genderLabel',
			},
			{
				label: '年龄',
				prop: 'age',
			},
			{
				label: '联系方式',
				prop: 'oldmanPhone',
			},
			{
				label: '政府扶助对象申请时间',
				prop: 'createTime',
			},
			{
				label: '老人类型',
				prop: 'elderlyTypeLabel',
			},
			{
				label: '评估机构评估结果',
				prop: 'assessAbilityLevel',
				propName: 'assessAbilityLevelLabel',
				custome: 'tag',
				dicData: [
					{ label: '自理', value: 1, type: 'primary' },
					{ label: '半失能', value: 2, type: 'warning' },
					{ label: '失能', value: 3, type: 'danger' },
					{ label: '失智', value: 4, type: 'danger' },
				],
			},
			{
				label: '备注',
				prop: 'note',
			},
			{
				label: '操作',
				prop: 'menu',
				width: 300,
				fixed: 'right',
			},
		],
	},
}

let editMode = {
	crAreaSame: false,
	orgCrArea: true,
	elderCrArea: true,
}
const emit = defineEmits(['backData', 'getMonthVal'])
let props = withDefaults(
	defineProps<{
		otherData?: any
		applyDate?: any
		nowRowData?: any
		monthNum?: string
		isDetail?: boolean
		isPigeonhole?: boolean // 是否是归档，归档数据人员名单就不能动态的去获取了
	}>(),
	{
		otherData: {},
		applyDate: '',
		nowRowData: {},
		monthNum: '',
		isDetail: false,
		isPigeonhole: false,
	}
)
let tableKey = ref(0)
let tableData = ref([])
// 审批专用
let adiutConfig = cloneDeep(newBaseConfig)
adiutConfig.tableConfig.columns.shift()
adiutConfig.api = { index: 'view_ofsSubsidyApply_elderly_amount_page' }
adiutConfig.auth = { index: 'view_ofsSubsidyApply_elderly_amount_page' }
// 归档专用配置
let pigeonholeConfig = cloneDeep(newBaseConfig)
delete pigeonholeConfig.searchConfig
pigeonholeConfig.tableConfig.columns.shift()
pigeonholeConfig.api = { index: '' }
pigeonholeConfig.auth = { index: '' }


const onSelectionChange = (data) => {
	selected = data
}
let crud = ref()
const handleRefresh = (query?: any) => {
	nextTick(() => {
		crud.value.refreshChange(query)
	})
}
// 确认添加
const toSureAdd = () => {
	let params = JSON.parse(JSON.stringify(selected))
	params.forEach((el) => {
		delete el.id
		delete el.applyId
		delete el.applyNo
	})
	apiMod.view_ofsSubsidyAmountDetail_save_batch(params).then((res) => {
		handleRefresh()
		// getPersonData()
		table.value.clearSelection()
		emit('backData', list)
		showKey.value = Math.random()
	})
}
let selectedMonth = ref(0)
let tabKey = ref(0)
let mothList = reactive([
	{ label: '4月', value: 0, other: '', prop: '' },
	{ label: '5月', value: 1, other: '', prop: '' },
	{ label: '6月', value: 2, other: '', prop: '' },
	{ label: '7月', value: 3, other: '', prop: '' },
	{ label: '8月', value: 4, other: '', prop: '' },
	{ label: '9月', value: 4, other: '', prop: '' },
])

//  判断年份月份
const toGetMonthList = () => {
	if (props.nowRowData) {
		let date1 = props.nowRowData.oneVal.split('-')
		let date2 = props.nowRowData.twoVal.split('-')
		let date3 = props.nowRowData.threeVal.split('-')
		let date4 = props.nowRowData.fourVal.split('-')
		let date5 = props.nowRowData.fiveVal.split('-')
		let date6 = props.nowRowData.sixVal.split('-')
		mothList = [
			{ label: date1[0] + '年' + date1[1] + '月', value: 0, other: props.nowRowData.oneVal, prop: 'one' },
			{ label: date2[0] + '年' + date2[1] + '月', value: 1, other: props.nowRowData.twoVal, prop: 'two' },
			{ label: date3[0] + '年' + date3[1] + '月', value: 2, other: props.nowRowData.threeVal, prop: 'three' },
			{ label: date4[0] + '年' + date4[1] + '月', value: 3, other: props.nowRowData.fourVal, prop: 'four' },
			{ label: date5[0] + '年' + date5[1] + '月', value: 4, other: props.nowRowData.fiveVal, prop: 'five' },
			{ label: date6[0] + '年' + date6[1] + '月', value: 5, other: props.nowRowData.sixVal, prop: 'six' },
		]
		
		if (props.monthNum == 'one') {
			selectedMonth.value = 0
		} else if (props.monthNum == 'two') {
			selectedMonth.value = 1
		} else if (props.monthNum == 'three') {
			selectedMonth.value = 2
		} else if (props.monthNum == 'four') {
			selectedMonth.value = 3
		} else if (props.monthNum == 'five') {
			selectedMonth.value = 4
		} else if (props.monthNum == 'six') {
			selectedMonth.value = 5
		}
		// 当为归档时需要按申请年月划分死数据
		if(props.isPigeonhole){
			tableData.value = JSON.parse(props.otherData.allData.extendJson) || []
			pigeonholeConfig.tableConfig['data'] = tableData.value[mothList[selectedMonth.value].other]
		}
		
		// tabKey.value = Math.random()
	}
}
toGetMonthList()

let paramsObj = reactive({
	belongDateList: [props.applyDate],
	subsidyType: 9,
	applyType: 1,
	creditCode: props.nowRowData.creditCode,
	authCommunity:undefined
})

let paramsObj1 = reactive({
	belongDateList: [props.applyDate],
	subsidyType: 6,
	applyType: 2,
	creditCode: props.nowRowData.creditCode,
	amountSubsidyType: 9,
	amountApplyType: 1,
})
const handleClick = (item) => {
	selectedMonth.value = item.value
	paramsObj['belongDateList'] = [item.other]
	paramsObj1['belongDateList'] = [item.other]
	if(props.isPigeonhole){
		pigeonholeConfig.tableConfig['data'] = tableData.value[mothList[selectedMonth.value].other]
	}
	emit('getMonthVal', item)
	tableKey.value = Math.random()
}
let drawer = ref(false)
let listData = reactive([])
const toApply = () => {
	let arr = []
	list.forEach((el) => {
		arr.push(...el.child)
	})
	listData.forEach((el, index) => {
		arr.forEach((e) => {
			if (el.id == e.id) {
				listData.splice(index, 1)
			}
		})
	})
	drawer.value = true
}

let activeNames = ref(1)
const handleChange = () => {}
let dialogVisible = ref(false)
let copyRow = reactive({})
const openDalog = (row?) => {
	dialogVisible.value = true
	copyRow = row
}
//

const deleteRow = (ind, row) => {
	let item = personList.splice(ind, 1)
	apiMod.view_ofsSubsidyAmountDetail_del([row.amountDetailId]).then((res) => {
		// getPersonData()
	})
	// listData.push(...item)
	emit('backData', list)
	showKey.value = Math.random()
}

let showKey = ref(1)
let userInfo = getStore('userInfo') || {}
let list = reactive([
	{ area: '市辖区', areaId: '199555', num: 0, child: [] },
	{ area: '玄武区', areaId: '199556', num: 0, child: [] },
	{ area: '秦淮区', areaId: '199625', num: 0, child: [] },
	{ area: '建邺区', areaId: '199749', num: 0, child: [] },
	{ area: '鼓楼区', areaId: '199824', num: 0, child: [] },
	{ area: '浦口区', areaId: '199961', num: 0, child: [] },
	{ area: '栖霞区', areaId: '200102', num: 0, child: [] },
	{ area: '雨花台区', areaId: '200258', num: 0, child: [] },
	{ area: '江宁区', areaId: '200360', num: 0, child: [] },
	{ area: '六合区', areaId: '200582', num: 0, child: [] },
	{ area: '溧水区', areaId: '200754', num: 0, child: [] },
	{ area: '高淳区', areaId: '200876', num: 0, child: [] },
])
let sumTotal = computed(() => {
	let sum = list.reduce((sum, b) => {
		return sum + b.num
	}, 0)
	return sum
})

// 勾选的数据
let selected = reactive([])
const selectedData = (data) => {
	selected = data
	// 批量添加老人
}
let table = ref()

let personList = reactive([])

let pageReac = ref({
	current: 1,
	size: 5,
	total: 0,
})
let pageReac2 = ref({
	current: 1,
	size: 5,
	total: 0,
})
const onPageChange = (data) => {
	pageReac.value = data
	// getPersonData()
}

// 获取待申请列表
let drawerKey = ref(0)
const readPersonList = () => {
	let params = {
		belongDateList: [props.applyDate],
		subsidyType: 9,
		applyType: 1,
		creditCode: props.nowRowData.creditCode,
		pageParaPO: pageReac2.value,
	}
	apiMod.view_ofsSubsidyApply_elderly_amount_list(params).then((res) => {
		listData = res.data.records
		drawerKey.value = Math.random()
	})
}
// 获取人员数据

// getPersonData()

const onOperateFormate = (data, type) => {
	return new Promise((resolve, reject) => {
		if (type == 'detail') {
			console.log('点击进入')
			// visible.value = true
			// apiMod.view_ofsSubsidyApply_view(data.applyId).then((res) => {
			// 	rowData.value = res.data || {}
			// })
			// operateType.value = type
		} else {
			resolve(data)
		}
	})
}
</script>
<style scoped lang="scss">
.fr_str {
	display: flex;
	flex-direction: row;
	justify-content: flex-start;
	align-items: center;
}
.add_part {
}
.page_part {
	margin-top: 20px;
	width: 100%;
	display: flex;
	flex-direction: row-reverse;
	align-items: center;
}
.btn_oart {
	margin: 0 auto;
	width: 200px;
	display: flex;
	flex-direction: row;
	justify-content: space-between;
	align-items: center;
}
</style>
